<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="人员部别">
              <a-tree-select
                v-model="queryParam.unit"
                v-decorator="['unit', {rules: [{required: true, message: '请选择部别'}]}]"
                style="width: 100%"
                :tree-data="treeData"
                :dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
                placeholder="请选择"
                allow-clear
                :treeDefaultExpandedKeys="expandedKeys"
              />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="人员姓名">
              <a-input v-model="queryParam.personName" placeholder=""/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="24">
              <a-form-item label="证件号">
                <a-input v-model="queryParam.personCard" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="人员类别">
                <a-select v-model="queryParam.personType">
                  <a-select-option :value="null">全部人员</a-select-option>
                  <a-select-option value="军官">军官队伍</a-select-option>
                  <a-select-option value="军士">军士队伍</a-select-option>
                  <a-select-option value="兵">兵队伍</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="军衔">
                <a-select v-model="queryParam.personMilitaryRank">
                  <a-select-option :key="i" v-for="(military, i) in militaryList" :value="military">
                    {{ military }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="政治面貌">
                <a-select v-model="queryParam.personPoliticsStatus">
                  <a-select-option value="中共党员">中共党员</a-select-option>
                  <a-select-option value="中共预备党员">中共预备党员</a-select-option>
                  <a-select-option value="共青团员">共青团员</a-select-option>
                  <a-select-option value="群众">群众</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="文化程度">
                <a-select v-model="queryParam.personEduLevel">
                  <a-select-option value="初中">初中</a-select-option>
                  <a-select-option value="高中">高中</a-select-option>
                  <a-select-option value="中专">中专</a-select-option>
                  <a-select-option value="大专">大专</a-select-option>
                  <a-select-option value="本科">本科</a-select-option>
                  <a-select-option value="硕士">硕士</a-select-option>
                  <a-select-option value="博士">博士</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addSoldier.add()">新建</a-button>
      <a-button type="primary" icon="cloud-upload" @click="$refs.importBasic.show()">基础数据导入</a-button>
      <a-button type="primary" icon="cloud-upload" @click="$refs.importExperience.show()">主要经历数据导入</a-button>
      <a-button type="primary" icon="cloud-upload" @click="$refs.importRelation.show()">社会关系数据导入</a-button>
      <a-button type="primary" icon="download" @click="$refs.outputExcel.show()">人员数据导出</a-button>
<!--      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>-->
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="remove">
            <remove-Soldier ref="removeSoldier" :personIds="selectedRowKeys.join(',')" @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
      <a-dropdown  v-action:edit v-else-if="selectedRowKeys.length == 0">
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
      <a-button @click="nowCol = 1 - nowCol">调整顺序</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns[nowCol]"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    >
      <template slot="personOrder" slot-scope="text, record">
        <preview-and-edit
          v-if="paeShow"
          :id="record.key"
          :default-value="text"
          :save="saveData"
          type="number"
          @ok="(val)=>record.personOrder=val"/>
      </template>
      <span slot="unitId" slot-scope="text">
        <a-tag color="#108ee9">{{ text | unitTextFilter }}</a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="showInfo(record)">详情</a>
          <a-divider type="vertical" />
<!--          <a @click="handleImage(record)">照片</a>-->
<!--          <a-divider type="vertical" />-->
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <remove-soldier ref="removeSoldier" :personIds="String(record.personId)" @ok="handleOk"/>
        </template>
      </span>
    </s-table>
    <add-soldier ref="addSoldier" @ok="handleOk" />
    <soldier-info ref="soldierInfo" @ok="handleOk"/>
    <edit-soldier ref="editSoldier" @ok="handleOk"/>
    <change-image ref="changeImage" @ok="handleOk"/>
    <import-basic ref="importBasic" @ok="handleOk"/>
    <import-experience ref="importExperience" @ok="handleOk"/>
    <import-relation ref="importRelation" @ok="handleOk"/>
    <output-excel ref="outputExcel" @ok="handleOk"></output-excel>
    <experience ref="experience" @ok="handleOk"/>
    <experience2 ref="experience2" @ok="handleOk"/>
    <social-relation ref="socialRelation" @ok="handleOk"/>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '@/components'
import { querySoldier, updatePersonOrder } from '@/api/basic/soldier'
import { listUnit, listUnitTree } from '@/api/basic/unit'
import { TreeSelect } from 'ant-design-vue'
import AddSoldier from './modules/soldier/AddSoldier'
import SoldierInfo from '@/views/basic/modules/soldier/SoldierInfo'
import EditSoldier from './modules/soldier/EditSoldier'
import RemoveSoldier from './modules/soldier/RemoveSoldier'
import ChangeImage from './modules/soldier/ChangeImage'
import ImportBasic from './modules/soldier/ImportBasic'
import ImportExperience from './modules/soldier/ImportExperience'
import ImportRelation from './modules/soldier/ImportRelation'
import Experience from '@/views/basic/modules/soldier/Experience'
import Experience2 from '@/views/basic/modules/soldier/Experience2'
import SocialRelation from '@/views/basic/modules/soldier/SocialRelation'
import OutputExcel from './modules/soldier/OutputExcel'
import PreviewAndEdit from '@/components/PreviewAndEdit/PreviewAndEdit'
// 用于处理过滤器动态获取数据
let that

export default {
  name: 'SoldierList',
  components: {
    PreviewAndEdit,
    OutputExcel,
    STable,
    AddSoldier,
    SoldierInfo,
    EditSoldier,
    RemoveSoldier,
    ChangeImage,
    ImportBasic,
    ImportExperience,
    ImportRelation,
    Experience,
    Experience2,
    SocialRelation,
    ATreeSelect: TreeSelect
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 角色对象
      units: [],
      treeData: [],
      expandedKeys: [1],
      militaryList: ['列兵', '上等兵', '下士', '中士', '上士', '四级军士长', '三级军士长', '二级军士长',
        '一级军士长', '少尉', '中尉', '上尉', '少校', '中校', '上校', '大校', '少将', '中将', '上将'],
      nowCol: 0,
      // 表头
      columns: [[
        {
          title: '证件号',
          dataIndex: 'personCard',
          align: 'center'
        },
        {
          title: '姓名',
          dataIndex: 'personName',
          align: 'center'
        },
        {
          title: '部别',
          dataIndex: 'unitId',
          scopedSlots: { customRender: 'unitId' },
          align: 'center',
          sorter: true
        },
        {
          title: '军衔',
          dataIndex: 'personMilitaryRank',
          align: 'center'
        },
        {
          title: '职级',
          dataIndex: 'personRank',
          align: 'center'
        },
        {
          title: '政治面貌',
          dataIndex: 'personPoliticsStatus',
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ], [
        {
          title: '证件号',
          dataIndex: 'personCard',
          align: 'center'
        },
        {
          title: '姓名',
          dataIndex: 'personName',
          align: 'center'
        },
        {
          title: '部别',
          dataIndex: 'unitId',
          scopedSlots: { customRender: 'unitId' },
          align: 'center',
          sorter: true
        },
        {
          title: '军衔',
          dataIndex: 'personMilitaryRank',
          align: 'center'
        },
        {
          title: '职级',
          dataIndex: 'personRank',
          align: 'center'
        },
        {
          title: '政治面貌',
          dataIndex: 'personPoliticsStatus',
          align: 'center'
        },
        {
          title: '顺序',
          dataIndex: 'personOrder',
          align: 'center',
          scopedSlots: { customRender: 'personOrder' },
          width: 150
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ]],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        this.paeShow = false
        return querySoldier(Object.assign(parameter, queryJson))
          .then(res => {
            this.paeShow = true
            return res.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true,
      saveData: (id, val) => {
        return updatePersonOrder({ id: id, order: val }).then(res => {
          return res.message === 'success'
        }).catch(() => false)
      },
      paeShow: true
    }
  },
  filters: {
    unitTextFilter (unitId) {
      let unitName = ''
      for (const unit of that.units) {
        if (parseInt(unit.unitId) === parseInt(unitId)) {
          unitName = unit.unitName
          break
        }
      }
      return unitName
    }
  },
  beforeCreate () {
    that = this
  },
  created () {
    // this.tableOption()
    listUnitTree()
      .then(res => {
        if (res.message !== 'error') {
          this.treeData = res.result
        } else {
          this.$message.error('操作失败！')
        }
      })
    listUnit()
      .then(res => {
        if (res.message !== 'error') {
          this.units = res.result
        } else {
          this.$message.error('操作失败！')
        }
      })
  },
  methods: {
    // tableOption () {
    //   if (!this.optionAlertShow) {
    //     this.options = {
    //       alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
    //       rowSelection: {
    //         selectedRowKeys: this.selectedRowKeys,
    //         onChange: this.onSelectChange
    //       }
    //     }
    //     this.optionAlertShow = true
    //   } else {
    //     this.options = {
    //       alert: false,
    //       rowSelection: null
    //     }
    //     this.optionAlertShow = false
    //   }
    // },
    removeSoldier (personId) {
      this.$refs.removeSoldier.handleSubmit(personId)
    },
    handleEdit (record) {
      this.$refs.editSoldier.edit(record)
    },
    showInfo (record) {
      this.$refs.soldierInfo.info(record)
    },
    handleImage (record) {
      this.$refs.changeImage.upload(record)
    },
    handleExperience1 (record) {
      this.$refs.experience.experience(record)
    },
    handleExperience2 (record) {
      this.$refs.experience2.experience(record, 2)
    },
    handleExperience3 (record) {
      this.$refs.experience2.experience(record, 3)
    },
    handleSocialRelation1 (record) {
      this.$refs.socialRelation.socialRelation(record, 1)
    },
    handleSocialRelation2 (record) {
      this.$refs.socialRelation.socialRelation(record, 2)
    },
    handleSocialRelation3 (record) {
      this.$refs.socialRelation.socialRelation(record, 3)
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>
